<?php
/**
 * @修改：Chris
 * @时间:    2018-09-03 11:37:26
 * @功能: 销货单
 */
header('Access-Control-Allow-Origin: *');
require( "config.php");
$op = $_POST['op'];

//现金收款
if($op == 'cash'){
	/*插入账户明细表*/
	unset($_POST['op']);
	$account['billNo'] = '';//单号
	$account['billType'] = 'RECEIPT';
	$account['billDate'] = date('Y-m-d');
	$account['payment'] = '';//交易金额
	$account['remark'] = '现金收款';//备注
	$account['transType'] = '153001';
	$account['transType'] = '收款';
	$account['companyId'] = '';//商户ID
	$id = $db->insert('ci_account_info',$account);

	/*插入发票记录表*/
	$invoice['billNo'] = '';//单号
	$invoice['transType'] = '153001';
	$invoice['rpAmount'] = '';//交易金额
	$invoice['billDate'] = date('Y-m-d');
	$invoice['arrears'] = '';//负的交易金额
	$invoice['billType'] = 'RECEIPT';
	$invoice['transTypeName'] = '收款';
	$invoice['companyId'] = '';//商户ID
	$id2 = $db->insert('ci_invoice',$invoice);

	if($id && $id2){
		die(json_encode('添加成功'));
	}else{
		die(json_encode('添加失败'));
	}

}

// insert
if($op == 'add'){
unset($_POST['op']);
$invoice = $_POST;
unset($invoice['invId']);
unset($invoice['price']);
unset($invoice['qty']);
$invoice['billType'] = 'SALE';
$invoice['hxStateCode'] = '2';//'0未付款  1部分付款  2全部付款',
$invoice['modifyTime'] = date('Y-m-d H:i:s');

//拆分生成数组
$_POST['companyId'] = explode(',', $_POST['companyId']);
$_POST['amount'] = explode(',', $_POST['amount']);
$_POST['invId'] = explode(',', $_POST['invId']);
$_POST['price'] = explode(',', $_POST['price']);
$_POST['qty'] = explode(',', $_POST['qty']);

//收入表
$amount['billType'] = $invoice['billType'] = $info['billType'] = 'SALE';//单据类型:售货
$amount['transTypeName'] = $invoice['transTypeName'] = $info['transTypeName'] = '销货'; //单据类型名称
$amount['transType'] = $invoice['transType'] = $info['transType'] = '150601';//单据类型编号
$amount['billDate'] = $invoice['billDate'] = $info['billDate'] = date('Y-m-d');//时间
// $amount['accId'] = 1;//结算账户ID
foreach ($_POST['companyId'] as $key => $value) {
	if($value!=''){
		$invoice['companyId'] = $value;
		$invoice['amount'] = $invoice['rpAmount'] = $_POST['amount'][$key];
		$id[] = $db->insert('ci_invoice',$invoice);
	}
}

//账户信息表
// $amount['buId'] = $invoice['buId'] = $_POST['salesId'];//客户ID
// $amount['buId'] = $_POST['salesId'];
$info['billNo'] = $amount['billNo'] = $_POST['billNo'];
foreach ($_POST['companyId'] as $key1 => $value1) {
	if($value1!=''){
		$amount['companyId'] = $value1;
		$amount['iid'] = $id[$key1];
		$amount['payment'] = $_POST['amount'][$key1];
		$id2[] = $db->insert('ci_account_info',$amount);
	}
}

foreach ($_POST['invId'] as $key2 => $value2) {
	if($value2!=''){
		$info['qty'] = $_POST['qty'][$key2]*-1; //购买数量
		$info['invId'] = $_POST['invId'][$key2]; //商品ID
		$info['iid'] = $id[$key2];//发票ID
		$info['price'] = $_POST['price'][$key2];//单价
		$info['amount'] = $_POST['amount'][$key2];//总价

		$id3 = $db->insert('ci_invoice_info',$info);
	}
}

 if($id && $id2 && $id3){
	$re['status'] = '200';
	$re['msg'] = $id;
 }else{
	$re['status'] = '110';
	$re['msg'] = '添加失败！';
 }
 die(json_encode($re));
 }

// update
if($op == 'update'){

 $id =  $_POST['id'];
$data = $_POST;
unset($data['op']);

 $where = 'id = '.$id;
 $id = $db->update('ci_invoice',$data,$where);

 if($id){
	$re['status'] = '200';
	$re['msg'] =  $op.' success!';
 }else{
	$re['status'] = '110';
	$re['msg'] = $op.' failed!';;
 }
 	die(json_encode($re));
 }

//delete
 if($op == 'delete'){

 $id =  $_POST['id'];
 $where = 'id = '.$id;
 $id = $db->delete('ci_invoice',$where);

 if($id){
	$re['status'] = '200';
	$re['msg'] =  $op.' success!';
 }else{
	$re['status'] = '110';
	$re['msg'] = $op.' failed!';;
 }
 	die(json_encode($re));
 }

//find
 if($op == 'find'){

 $id =  $_POST['id'];
 $where = 'id = '.$id;
  $db->find('ci_invoice','*',$where);
  $re1 = $db->fetch_assoc();

 if($re1){
	$re['status'] = '200';
	$re['msg'] =  $re1;
 }else{
	$re['status'] = '110';
	$re['msg'] = $op.' failed!';;
 }
 	die(json_encode($re));
 }

//select
 if($op == 'select'){

 $where =  $_POST['where'];
 $re1 = $db->select('ci_invoice','*',$where);

 if($re1){
	$re['status'] = '200';
	$re['msg'] =  $re1;
 }else{
	$re['status'] = '110';
	$re['msg'] = $op.' failed!';;
 }
 	die(json_encode($re));
 }
 die('no op!');
